//
//  ViewControllerHome.swift
//  AirlineReservation
//
//  Created by Kevin on 2019/12/26.
//  Copyright © 2019 Kevin. All rights reserved.
//

import UIKit

class VCHome: VCBase {
    
    // 轮播图
    private lazy var viewPager : ViewPagerView = {
        let ui = ViewPagerView(frame: CGRect(x: MARGIN, y: realHeight, width: S_16_M, height: VIEWPAGER_HEIGHT))
        return ui
    }()
    
    // 机票相关
    private lazy var ticketView: TicketMangerView = {
        let uiY = viewPager.frame.maxY + MARGIN
        let ui = TicketMangerView(frame: CGRect(x: 0, y: uiY, width: SCREEN_WIDTH, height: MARGIN110))
        return ui
    }()
    
    // 特价机票头部
    private lazy var specialHeaderView: SpecialHeaderView = {
        let ui = SpecialHeaderView()
        ui.text = "特价机票"
        let uiY = ticketView.frame.maxY + MARGIN10
        ui.frame = CGRect(x: 0, y: uiY, width: SCREEN_WIDTH, height: 60)
        ui.backgroundColor = UIColor.white
        return ui
    }()
    
    // 特价机票滑动UI
    private lazy var specialScrollView: SpecialScrollView = {
        let uiY = specialHeaderView.frame.maxY
        let ui = SpecialScrollView()
        ui.frame = CGRect(x: 0, y: uiY, width: SCREEN_WIDTH, height: 180)
        return ui
    }()
    
    // 热门航线头部
    private lazy var hotHeaderView: SpecialHeaderView = {
        let ui = SpecialHeaderView()
        ui.frame = CGRect(x: 0, y: specialScrollView.frame.maxY + MARGIN10, width: SCREEN_WIDTH, height: 60)
        ui.text = "热门航线"
        ui.backgroundColor = UIColor.white
        return ui
    }()
    
    // 热门航线图片
    private lazy var hotTicketView: HotTikctView = {
        let ui = HotTikctView()
        ui.frame = CGRect(x: 0, y: hotHeaderView.frame.maxY, width: SCREEN_WIDTH, height: 320)
        ui.backgroundColor = UIColor.white
        return ui
    }()
    
    // 会员专享头部
    private lazy var memberHeaderView: SpecialHeaderView = {
        let ui = SpecialHeaderView()
        ui.frame = CGRect(x: 0, y: hotTicketView.frame.maxY + MARGIN10, width: SCREEN_WIDTH, height: 60)
        ui.text = "会员专享"
        ui.moreText = "查看全部"
        ui.backgroundColor = UIColor.white
        return ui
    }()
    
    // 会员专享
    private lazy var memberItemView: MemberItemView = {
        let ui = MemberItemView(frame: CGRect(x: 0, y: memberHeaderView.frame.maxY, width: SCREEN_WIDTH, height: 300))
        ui.backgroundColor = UIColor.white
        return ui
    }()
    
    // scrollView
    private lazy var scrollView: UIScrollView = {
        let uiY = SCREEN_HEIGHT - realHeight
        let ui = UIScrollView(frame: CGRect(x: 0, y: realHeight, width: SCREEN_WIDTH, height: uiY))
        return ui
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        init_set()
        init_view()
        init_data()
        init_event()
    }
}

// MARK: 逻辑部分
extension VCHome {
    
    // 能否在线选座
    private func isSelectSeat() -> Void {
        let permissionid = UserDefaults.standard.integer(forKey: SP_PERMISSIONID)
        if permissionid == 1 {
            self.showDialog(str: "对不起，该功能仅对会员开放")
            return
        }
        if permissionid == 3 {
            self.showDialog(str: "对不起，您的会员已过期")
            return
        }
        self.pushVc(title: "在线选座", vc: VCSeat())
    }
    
    func init_event() -> Void {
        // TODO 轮播图回调
        viewPager.complete = { item in
            let vc = VCWeb()
            vc.pageapi = "viewpager/\(item)"
            self.pushNextVcHideShowBottomBar(vc: vc)
        }
        
        // TODO 机票相关回调
        ticketView.complete = { item in
            switch item {
            case 0:
                self.pushNextVcHideShowBottomBar(vc: VCSearch())
            case 1:
                self.pushVc(title: "改签票", vc: VCChange())
            case 2:
                self.pushVc(title: "退票", vc: VCRefund())
            case 3:
                self.isSelectSeat()
            default:
                break
            }
        }
        
        // TODO 特价更多回调
        specialHeaderView.complete = {
            let vc = VCMore()
            vc.title = "特价机票"
            self.pushNextVcHideShowBottomBar(vc: vc)
        }
        
        // TODO 特价滑动UI回调
        specialScrollView.complete = { item in
            let dict = NSMutableDictionary()
            dict.setValue(item.city, forKey: "city");
            dict.setValue(item.endcity, forKey: "endcity");
            dict.setValue(item.departdate, forKey: "departdate");
            dict.setValue(item.flightnumberid, forKey: "flightnumberid");
            // 跳转界面
            let vc = VCShowTicket()
            vc.requestDict = dict
            vc.title = "\(item.city!) - \(item.endcity!)"
            self.pushNextVcHideShowBottomBar(vc: vc)
        }
        
        // 热门航线图片回调
        hotTicketView.complete = { item in
            let vc = VCWeb()
            vc.pageapi = "hotline/\(item)"
            self.pushNextVcHideShowBottomBar(vc: vc)
        }
        
        // 会员专享 全部
        memberHeaderView.complete = {
            let vc = VCMember()
            vc.title = "会员专享"
            self.pushNextVcHideShowBottomBar(vc: vc)
        }
        
        // 会员专享 选项
        memberItemView.comoplete = {item in
            let vc = VCWeb()
            vc.pageapi = "member/\(item)"
            self.pushNextVcHideShowBottomBar(vc: vc)
        }
    }
}

// MARK: 初始化
extension VCHome {
    
    func init_data() -> Void {
        // 拼接数据
        let dict = NSMutableDictionary()
        dict.setValue(4, forKey: "count")
        // 请求
        self.doPost(api: "search/specailticket", dict: dict,isLoading: false) { (dict) in
            if self.isServerSuccess(dict: dict) != 1200 {
                return
            }
            // 解析数据
            guard let result = dict.value(forKey: SP_BODYREQUEST) as? NSDictionary else {
                return
            }
            // 特价机票
            if let specials = result.value(forKey: "specials") as? NSArray {
                var temp = [SpecialTicketInfo]()
                for item in specials {
                    let ticket = SpecialTicketInfo(dict: item as! [String : Any])
                    temp.append(ticket)
                }
                self.specialScrollView.specialTickets = temp
            }
            // 会员专享
            if let zones = result.value(forKey: "zones") as? NSArray {
                var temp = [MemberZoneInfo]()
                for item in zones {
                    let zone = MemberZoneInfo(dict: item as! [String : Any])
                    temp.append(zone)
                }
                self.memberItemView.memberZones = temp
            }
        }
    }
    
    func init_view() -> Void {
        scrollView.addSubview(viewPager)
        scrollView.addSubview(ticketView)
        scrollView.addSubview(specialHeaderView)
        scrollView.addSubview(specialScrollView)
        scrollView.addSubview(hotHeaderView)
        scrollView.addSubview(hotTicketView)
        scrollView.addSubview(memberHeaderView)
        scrollView.addSubview(memberItemView)
        // 添加到父控件
        view.addSubview(scrollView)
    }
    
    func init_set() -> Void {
        // 热门航线不显示更多文本
        hotHeaderView.isShowMore = false
        // 机票相关背景设置为白色
        ticketView.backgroundColor = UIColor.white
        // scrollView滑动范围
        scrollView.contentSize = CGSize(width: SCREEN_WIDTH, height: memberItemView.frame.maxY + MARGIN)
        // scrollView属性设置
        scrollView.bounces = false
        scrollView.showsVerticalScrollIndicator = false
        scrollView.contentOffset = CGPoint(x: 0, y: realHeight)
        scrollView.contentInset = UIEdgeInsets(top: -realHeight, left: 0, bottom: 0, right: 0)
    }
}
